package com.itheima.stock.config;

import com.itheima.stock.pojo.vo.StockInfoConfig;
import com.itheima.stock.pojo.vo.TaskThreadPoolInfo;
import com.itheima.stock.utils.IdWorker;
import com.itheima.stock.utils.ParserStockInfoUtil;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author : itheima
 * @date : 2023/8/31 8:42
 * @description :
 */
@Configuration
@EnableConfigurationProperties({StockInfoConfig.class, TaskThreadPoolInfo.class})
public class CommonConfig {
    @Value("${data.datacenterId}")
    private Long datacenterId;
    @Value("${data.workerId}")
    private Long workerId;

    /**
     * 定义http客户端工具bean
     * @return
     */
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * 维护分布式id生成工具bean
     * @return
     */
    @Bean
    public IdWorker idWorker(){
        return new IdWorker(workerId,datacenterId);
    }

    /**
     * 定义构建解析原始股票相关数据的工具类
     * @param idWorker
     * @return
     */
    @Bean
    public ParserStockInfoUtil parserStockInfoUtil(IdWorker idWorker){
        return new ParserStockInfoUtil(idWorker);
    }

    /**
     * mq消息的格式转化
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


}
